package com.chanjiedata.changbs.module.login;
import android.graphics.Color;
import android.os.Bundle;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.chanjiedata.changbs.R;
import com.chanjiedata.changbs.base.BaseActivity;
import com.chanjiedata.changbs.net.Apis;
import com.chanjiedata.changbs.net.NetworkManager;
import com.chanjiedata.changbs.net.WebApis;
import com.chanjiedata.changbs.net.interfaces.OnRequestCallBack;
import com.chanjiedata.changbs.proto.Connect;
import com.chanjiedata.changbs.utlis.StatusBarUtil;
import com.chanjiedata.changbs.view.VerificationCodeEditText;
import com.chanjiedata.changbs.web.ui.WebViewActivity;
import com.google.protobuf.InvalidProtocolBufferException;

import butterknife.BindView;
import butterknife.ButterKnife;

public class RegisterCodeActivity extends BaseActivity {


    @BindView(R.id.back_tv)
    TextView backTv;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.input_code)
    VerificationCodeEditText inputCode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register_code);
        StatusBarUtil.setStatusBarLightMode(this, Color.parseColor("#ffffff"), true);
        ButterKnife.bind(this);
        tvTitle.setText("");
        backTv.setOnClickListener(v -> finish());
        inputCode.setOnInputListener(new VerificationCodeEditText.OnInputListener() {
            @Override
            public void OnEdittextChange(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void OnInputOk(String codeNum) {

                Connect.sms_register.Builder builder = Connect.sms_register.newBuilder();
                builder.setInviterId(codeNum);
                showProgress();
                NetworkManager.INSTANCE.post(Apis.check_inviter, builder.build().toByteArray(), new OnRequestCallBack() {
                    @Override
                    public void onOk(byte[] bytes) {
                        hideProgress();
                    }

                    @Override
                    public void onError(int code, String errorMessage) {
                        hideProgress();
                        if (code == 200) {
                            WebViewActivity.startActivity(mContext, WebApis.register + "?inviter_code=" + codeNum);
                            finish();
                        } else {
                            showToast(errorMessage);
                        }
                    }
                });
            }
        });
        showSoftInputFromWindow(inputCode.editCodeNum);
    }

    public void showSoftInputFromWindow(EditText editText){
        editText.setFocusable(true);
        editText.setFocusableInTouchMode(true);
        editText.requestFocus();
        this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    }
}
